package com.scsystem.xysc.controller;

import com.scsystem.xysc.aop.LogOperations;
import com.scsystem.xysc.pojo.Cart;
import com.scsystem.xysc.pojo.vo.ReturnResult;
import com.scsystem.xysc.service.CartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@Tag(name = "购物车接口")
@RequestMapping("/cart")
public class CartController {

    @Resource
    private CartService cartService;

    @PostMapping("/add")
    @Parameters({
            @Parameter(name = "uid", description = "用户id"),
            @Parameter(name = "pid", description = "商品id"),
            @Parameter(name = "quantity", description = "商品数量")
    })
    @Operation(summary = "添加商品到购物车")
    @LogOperations(module = "购物车", operation = "添加商品到购物车")
    public ReturnResult<Boolean> addCart(Long uid, Long pid, Integer quantity) {
        return cartService.addCart(uid, pid, quantity);
    }

    @DeleteMapping("/delete")
    @Parameters({
            @Parameter(name = "uid", description = "用户id"),
            @Parameter(name = "pid", description = "商品id")
    })
    @Operation(summary = "删除购物车中的商品")
    @LogOperations(module = "购物车", operation = "删除购物车中的商品")
    public ReturnResult<Boolean> deleteCart(Long uid, Long pid) {
        return cartService.deleteCart(uid, pid);
    }

    @PostMapping("/update")
    @Parameters({
            @Parameter(name = "uid", description = "用户id"),
            @Parameter(name = "pid", description = "商品id"),
            @Parameter(name = "quantity", description = "商品数量")
    })
    @Operation(summary = "修改购物车中的商品数量（不能为0）")
    @LogOperations(module = "购物车", operation = "修改购物车中的商品数量（不能为0）")
    public ReturnResult<Boolean> updateCart(Long uid, Long pid, Integer quantity) {
        return cartService.updateCart(uid, pid, quantity);
    }

    @GetMapping("/select")
    @Parameters({
            @Parameter(name = "uid", description = "用户id")
    })
    @Operation(summary = "查询用户购物车中的商品")
    @LogOperations(module = "购物车", operation = "查询用户购物车中的商品")
    public ReturnResult<List<Cart>> selectCart(Long uid) {
        return cartService.selectCart(uid);
    }

}